<style>
    #form {
        margin-top: 15px;
    }
</style>

<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <form action="" class="form-horizontal margin-top-big" id="form" method="post">
                <div class="form-group">
                    <label for="father_category" class="col-sm-2 control-label">父级类目</label>
                    <div class="col-sm-8">
                        <select id="category_1" name="category_1" class="col-sm-2"
                                data-value="{{isset($category["father"][1]) ? $category["father"][1]['id'] : ""}}">
                            <option value="0">一级类目</option>
                        </select>
                        <select id="category_2" name="category_2"
                                data-value="{{isset($category["father"][2]) ? $category["father"][2]['id'] : ""}}">
                            <option value=""></option>
                        </select>
                        <select id="category_3" name="category_3"
                                data-value="{{isset($category["father"][3]) ? $category["father"][3]['id'] : ""}}">
                            <option value=""></option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="brand_name" class="col-sm-2 control-label">类目名称</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control" name="cate_name"
                               value="{{isset($category["cate_name"]) ? $category["cate_name"] : "" }}">
                        <input type="hidden"
                               name="category_id"
                               value="{{isset($category["id"]) ? $category["id"] : ""}}">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">是否推荐</label>
                    <div class="col-sm-5">
                        <label class="radio-inline">
                            <input type="radio" name="suggest_status"
                                   value="0"
                                   @if ((isset($category['suggest_status']) && $category['suggest_status'] == 0) || !isset($category['suggest_status']))
                                   checked
                                    @endif> 不推荐
                        </label>
                        <label class="radio-inline">
                            <input type="radio" name="suggest_status" value="1"
                                   @if(isset($category['suggest_status']) && $category['suggest_status'] == 1)
                                   checked
                                    @endif> 推荐
                        </label>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">材质信息</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" name="material"
                               value="{{isset($category["material"]) ? $category["material"] : ""}}"
                               data-role="tagsinput" placeholder="添加材质标签">
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">尺寸信息</label>
                    <div class="col-sm-8">
                        <input type="text" class="form-control" name="size"
                               value="{{isset($category["size"]) ? $category["size"] : ""}}"
                               data-role="tagsinput"
                               placeholder="添加尺寸标签">
                    </div>
                </div>
                <div class="box-footer">
                    <div class="col-md-offset-4">
                        <button class="btn btn-primary col-md-1 save">保存</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="/vendor/js/select2/dist/js/select2.full.js"></script>
<link rel="stylesheet" href="/vendor/js/bootstrap-tagsinput/bootstrap-tagsinput.css">
<script src="/vendor/js/bootstrap-tagsinput/bootstrap-tagsinput.js"></script>

<script type="text/javascript">
    $(function () {
        $('#form').keypress(function (ev) {
            var ev = window.event || ev;
            if (ev.keyCode == 13 || ev.which == 13) {
                return false;
            }
        });
        $.getJSON("{{url('admin/category/info')}}", function (data) {
            var category_1 = $("#category_1");
            category_1.select2({
                placeholder: "一级类目",
                data: data,
                allowClear: true,
                width: "200px"
            });
            category_1.val(category_1.attr("data-value")).trigger('change');
        });
        $("#category_1").on("change", function (e) {
            var id = $(this).val();
            $.getJSON("{{url('admin/category/info')}}/" + (id?id:'-1'), function (data) {
                $("#category_2").html('').select2({
                    placeholder: "二级类目",
                    data: data,
                    allowClear: true,
                    width: "200px"
                });
                var lastCategoryId = $("#category_2").attr("data-value");
                if (lastCategoryId) {
                    $("#category_2").attr("data-value", "");
                    $("#category_2").val(lastCategoryId).trigger('change');
                } else {
                    $("#category_2").val("").trigger('change');
                }
            });

        });
        $("#category_2").on("change", function (e) {
            var id = $(this).val();
            $.getJSON("{{url('admin/category/info')}}/" + (id?id:'-1'), function (data) {
                $("#category_3").html('').select2({
                    placeholder: "三级类目",
                    data: data,
                    allowClear: true,
                    width: "200px"
                });
                var lastCategoryId = $("#category_3").attr("data-value");
                if (lastCategoryId) {
                    $("#category_3").attr("data-value", "");
                    $("#category_3").val(lastCategoryId).trigger('change');
                } else {
                    $("#category_3").val("").trigger('change');
                }
            });

        });
        $("#form").delegate(".save", "click", function () {
            var params = $("#form").serialize();
            $.post("{{url('admin/category/save')}}",params , function (result) {
                if (result.code == 0) {
                    alert("保存成功");
                    window.location.href = "{{url('admin/category/edit')}}/" + result.data.categoryId;
                } else {
                    alert("保存失败");
                }
            });
            return false;
        });
    })
    ;
</script>
